package com.baiyang.generator;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import com.baiyang.generator.conf.Configuration;
import com.baiyang.generator.utils.DealData;
import com.baiyang.generator.utils.FileUtil;
import com.baiyang.generator.utils.FreemarkerConfigUtil;

import freemarker.template.TemplateException;

/**
 * @design 获取配置文件，根绝配置文件获取数据，根绝模板和数据生成需要的代码
 * @explain 根据数据库表自动生成代码
 * @author XVX
 * @version 1.0
 * @since 2021-7-26
 */
public class App 
{
	private static Configuration configuration;
    public static void main( String[] args ) throws Exception
    {
        System.out.println( "Hello World!" );
        Map<String, Object> map = DealData.createMap();
        App.configuration = DealData.getConfiguration();
        createEntityFile(map);
        createControlFile(map);
        createInterfaceFile(map);
        createServiceFile(map);
        createDaoFile(map);
        createMapperFile(map);
    }
	public static void createEntityFile(final Map<String, Object> map) {
		try {
			FileUtil.generateToJava(FreemarkerConfigUtil.TYPE_ENTITY, map, 
					getPath(App.configuration.getPackageEntity()) , 
					App.configuration.getEntityClass() + ".java");
		} catch (IOException | TemplateException e) {
			e.printStackTrace();
		}
	}
	public static void createControlFile(final Map<String, Object> map) {
		try {
			FileUtil.generateToJava(FreemarkerConfigUtil.TYPE_CONTROLLER, map, 
					getPath(App.configuration.getPackageController()), 
					App.configuration.getControlClass() + ".java");
		} catch (IOException | TemplateException e) {
			e.printStackTrace();
		}
	}
	public static void createServiceFile(final Map<String, Object> map) {
		try {
			FileUtil.generateToJava(FreemarkerConfigUtil.TYPE_SERVICE, map, 
					getPath(App.configuration.getPackageInterface() + File.separator + App.configuration.getPackageService()), 
					App.configuration.getServiceClass() + ".java");
		} catch (IOException | TemplateException e) {
			e.printStackTrace();
		}
	}
	public static void createInterfaceFile(final Map<String, Object> map) {
		try {
			FileUtil.generateToJava(FreemarkerConfigUtil.TYPE_INTERFACE, map, 
					getPath(App.configuration.getPackageInterface()), 
					App.configuration.getInterfaceClass() + ".java");
		} catch (IOException | TemplateException e) {
			e.printStackTrace();
		}
	}
	public static void createDaoFile(final Map<String, Object> map) {
		try {
			FileUtil.generateToJava(FreemarkerConfigUtil.TYPE_DAO, map,
					getPath(App.configuration.getPackageDao()),
					App.configuration.getDaoClass() + ".java");
		} catch (IOException | TemplateException e) {
			e.printStackTrace();
		}
	}
	public static void createMapperFile(final Map<String, Object> map) {
		try {
			FileUtil.generateToJava(FreemarkerConfigUtil.TYPE_MAPPER, map, 
					App.configuration.getPathMapper() + File.separator, 
					App.configuration.getMapperName() + ".xml");
		} catch (IOException | TemplateException e) {
			e.printStackTrace();
		}
	}
	
	private static String getPath(String path) {
		return App.configuration.getProjectPath() + File.separator + path + File.separatorChar;
	}
}
